
package guoan.web.webCustomer;

import guoan.core.dto.RestResponse;
import guoan.domain.Menu;
import guoan.service.MenuService;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/webCustomer/menu")
public class WeiXinMenuController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(){
        return "/webCustomer/menu/index";
    }

    @RequestMapping(value = "data", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<List<Menu>> data(){
        return RestResponse.ok(menuService.findList());
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> update(Menu menu){
        return RestResponse.ok(menuService.update(menu));
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String toUpdate(Long id, Model model){
        boolean root = false;
        if (id != null) {
            Menu menu = menuService.findOne(id);
            model.addAttribute("menu", menu);
            root = menu.getRootId() == 0L;
        }
        if (!root) {
            List<Menu> rootMenus = menuService.findMenuByRootId(0L);
            model.addAttribute("rootMenus", rootMenus);
        }
        return "/webCustomer/menu/update";
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> del(Long id){
        return RestResponse.ok(menuService.delete(id));
    }

    @RequestMapping(value = "push", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> push(){
        return RestResponse.ok(menuService.push());
    }

}
